package com.example.demo.controller;

import com.example.demo.dao.DepartmentDao;
import com.example.demo.dao.EmployeeDao;
import com.example.demo.pojo.Department;
import com.example.demo.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collection;

@Controller
public class Edit {
    @Autowired
    DepartmentDao departmentDao;
    @Autowired
    EmployeeDao employeeDao;
    @GetMapping("/toedit")
    public String toaddpage(Model model ,@RequestParam("id") Integer id){
        Employee employee = employeeDao.Equery(id);
        Collection<Department> departments = departmentDao.getDepartment();
       model.addAttribute("departments",departments);
        model.addAttribute("emp",employee);
        return "edit";
    }
    @PostMapping("/edit")
    public String edit(Employee employee){
        employeeDao.save(employee);
        return "redirect:/login/list";
    }
    @GetMapping("/del")
    public String delete(@RequestParam("id") Integer id){
              employeeDao.delete(id);
        return "redirect:/login/list";
    }
}
